import Maxios from '../Http/MesHelp'

// 天气API接口类型定义
export interface WeatherResponse {
  success: boolean
  data: string
  coordinates?: {
    latitude: number
    longitude: number
  }
  cityName?: string
  timestamp: string
  message?: string
  error?: string
}

export interface CityInfo {
  code: string
  name: string
}

export interface CitiesResponse {
  success: boolean
  data: CityInfo[]
  count: number
  message: string
}

// 天气API服务类
export class WeatherService {
  /**
   * 通过经纬度获取天气预报
   * @param latitude 纬度 (-90 到 90)
   * @param longitude 经度 (-180 到 180)
   * @returns 天气信息
   */
  static async getForecastByCoordinates(
    latitude: number = 39.9042,
    longitude: number = 116.4074
  ): Promise<WeatherResponse> {
    try {
      const response = await Maxios.get('/api/Weather/GetForecast', {
        params: { latitude, longitude }
      })
      return response.data
    } catch (error) {
      console.error('获取天气数据失败:', error)
      throw error
    }
  }

  /**
   * 通过城市名获取天气预报
   * @param cityName 城市名称 (如: Beijing, Shanghai)
   * @returns 天气信息
   */
  static async getForecastByCity(cityName: string = 'Beijing'): Promise<WeatherResponse> {
    try {
      const response = await Maxios.get('/api/Weather/GetForecastByCity', {
        params: { cityName }
      })
      return response.data
    } catch (error) {
      console.error('获取城市天气数据失败:', error)
      throw error
    }
  }

  /**
   * 获取常用城市列表
   * @returns 城市列表
   */
  static async getCommonCities(): Promise<CitiesResponse> {
    try {
      const response = await Maxios.get('/api/Weather/GetCommonCities')
      return response.data
    } catch (error) {
      console.error('获取城市列表失败:', error)
      throw error
    }
  }
}

export default WeatherService
